package com.zhao.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.Menu;
import com.zhao.pojo.Role;
import com.zhao.service.MenuService;
import com.zhao.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;

    //角色数据list
    @RequestMapping("data_role")
    public String query(Model model) {
        List<Role> roles = roleService.list(null);
        model.addAttribute("roles", roles);
        return "admin/data_role";
    }
    //查询一条跳转修改页面
    @RequestMapping("editRole/{id}")
    public String editRole(@PathVariable("id") Integer id, Model model) {
        System.out.println(id);
        Role role = roleService.getById(id);
        model.addAttribute("role", role);
        return "admin/update_role";
    }
    //跳入添加页面
    @RequestMapping("addRole")
    public String addRole() {
        return "admin/add_role";
    }
    //添加修改
    @MyLog(value = "添加/修改角色")
    @ResponseBody
    @RequestMapping("saveRole")
    public String saveRole(Role role) {
        roleService.saveOrUpdate(role);
        return "1";
    }
    @RequestMapping("addPerms/{rid}")
    public String addPerms(@PathVariable("rid") Integer rid,Model model){
        System.out.println(rid);
        model.addAttribute("rid",rid);
        //查询根标题并排序
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("father","0");
        wrapper.orderByAsc("idx");
        List<Menu> menus = menuService.list(wrapper);
        //查询默认选中的标题集合
        List<String> checkeds=roleService.checkedList(rid);
        List<HashMap<String, Object>> result = new ArrayList<>();    //定义一个map处理json键名问题
        Object o = this.addPerms(menus, result, checkeds);
        System.out.println(o.toString());
        model.addAttribute("tree",o);

        return "admin/add_perms";
    }
   // @MyLog(value = "删除角色")
    @RequestMapping("delRole/{id}")
    public String delRole(@PathVariable("id") String id){
        roleService.delRole(id);
        return "redirect:/role/data_role";
    }
    //@MyLog(value = "角色授权")
    @ResponseBody
    @RequestMapping("/savePerms")
    public String savePerms(@RequestParam(value = "mids",required = false) String mids, @RequestParam("rid") String rid) {
        System.out.println("rid"+rid);
        System.out.println("mids"+mids);
        String[] ids = mids.split(",");
        roleService.savePerms(ids,rid);
        return "1";
    }
    //父标题集合 ，JSON格式存储对象，默认选中的集合
    public Object addPerms(List<Menu> menus, List<HashMap<String, Object>> result,List<String> checkeds) {
        for(Menu m : menus){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", m.getMenuId());
            map.put("title", m.getMname());
            map.put("spread", true);      //设置是否展开
            map.put("checked",checkeds.contains(m.getMenuId()+""));
            List<HashMap<String, Object>> result1 = new ArrayList<>();
            List<Menu> children = menuService.list(new QueryWrapper<Menu>().eq("father",m.getMenuId()));
            map.put("children", addPerms(children, result1,checkeds));
            result.add(map);
        }
        return result;
    }
}
